<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 	include("includes/db_connect.php");
	
	$zipCode = "";
	$nightMode = "";
	$nightStart = "";
	$nightEnd = "";
	$wizard = $_GET['wizard'];
	
	$weatherQuery = "SELECT zip_code,night_mode,night_start,night_end from tbl_weather";
	$weather=mysql_query($weatherQuery) or die(mysql_error());
	$weatherRow = mysql_fetch_array($weather);
	$zipCode = $weatherRow['zip_code'];
	$nightMode = $weatherRow['night_mode'];
	$nightStart = $weatherRow['night_start'];
	$nightEnd = $weatherRow['night_end'];
	
	if(isset($_POST['zipCode']))
	{
		$wizard = $_POST['wizard'];
		$zipCode = $_POST['zipCode'];
		if($_POST['nightMode'] == 'Y'){
			$nightMode = 'Y';
		}else{
			$nightMode = 'N';
		}
		$nightStart = $_POST['nightStart'];
		$nightEnd = $_POST['nightEnd'];
				
		$updateQuery = sprintf("update tbl_weather set zip_code = '%s', night_mode = '%s', night_start = '%s', night_end = '%s'",
			mysql_real_escape_string($zipCode),
			mysql_real_escape_string($nightMode),
			mysql_real_escape_string($nightStart),
			mysql_real_escape_string($nightEnd));
			
			$update=mysql_query($updateQuery) or die('error while trying to update: '.mysql_error());
			$message = "Weather settings successfully updated.";
	
		if($wizard == 'true'){
			//redirect to next step
			header("Location: movies-directory-edit.php?wizard=true");
		}else{
			//redirect to main setup with message
			header("Location: main-setup.php?message=".$message);
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<LINK href="../css/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<h4>Edit Weather Settings</h4>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input name="wizard" type="hidden" id="wizard" value="<?php echo $wizard;?>" />
<table cellpadding="2" class="rowmajor">
 <tbody>
   <tr>
     <th class="vt"><div align="left">Zip Code :</div></th>
     <td><input name="zipCode" type="text" id="zipCode" size="10" maxlength="10" value="<?php echo $zipCode;?>"/></td>
     <td>&nbsp;</td>
   </tr>
   <tr>
     <th class="vt"><div align="left">Show Moon at Night :</div></th>
     <td><label>
       <input name="nightMode" type="checkbox" id="nightMode" value="Y" <?php if($nightMode=='Y'){echo 'checked="checked"';}?>/>
       </label></td>
     <td>Check this if you want the icons to switch to a moon during nighttime </td>
   </tr>
   <tr>
     <th class="vt"><div align="left">Show Moon Start Time(24 hr clock):</div></th>
     <td><input name="nightStart" type="text" id="nightStart" size="5" maxlength="512" value="<?php echo $nightStart;?>"/></td>
     <td>Time of day you want to start showing moon (default 18:00) </td>
   </tr>
   <tr>
     <th class="vt"><div align="left">Show Moon End Time (24 hour clock):</div></th>
     <td><label>
     <input name="nightEnd" type="text" id="nightEnd" size="5" maxlength="5" value="<?php echo $nightEnd;?>"/>
     </label></td>
     <td>Time of day you want to stop showing moon (default 06:00) </td>
   </tr>
 </tbody></table>
<p>
 <?php
  	if($wizard == 'true'){
  ?>
  <input type="button" name="cancelButton" value="Back" onclick="location='main-edit.php?wizard=true'"/>
  <input type="submit" name="Submit" value="Next" />
  <?php 
  }else{
  ?>
  <input type="submit" name="Submit" value="Save" />
  <input type="button" name="cancelButton" value="Cancel" onclick="location='main-setup.php'"/>
  <?php
  }
  ?>
</p>
</form>
</body>
</html>
